package com.webserver.servlet;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Login  extends HttpServlet{
    public void service(HttpRequest httpRequest, HttpResponse httpResponse) {
        //读取
        String username = httpRequest.getQueryMap("username");
        String password = httpRequest.getQueryMap("password");
        //判断并跳转
        try (Connection connection=Jdbc.getCon()){
            String sql = "select id from user where username=? and password=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ResultSet rs = ps.executeQuery();
            //判断是否查询到了数据 查询到的话说明登录成功 否则登录失败
            if (rs.next()){//登录成功返回成功页面
                File file = new File("./webapps/myweb/login_success.html");
                httpResponse.setFile(file);
            }else{//登录失败返回失败页面
                File file = new File("./webapps/myweb/404.html");
                httpResponse.setFile(file);
            }
            System.out.println("注册完毕!");
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        System.out.println("登录成功");
    }
}
